﻿#include "messagestandarditemmodel.h"

MessageStandardItemModel::MessageStandardItemModel(QObject *parent) : QStandardItemModel(parent)
{
    headerClient<<"日期"
               <<"消息ID"
              <<"类型"
             <<"状态"
            <<"客户名称"
           <<"联系人"
          <<"电话"
         <<"订单号"
        <<"应开发票"
       <<"应收货款";

    headerGiver<<"日期"
              <<"消息ID"
             <<"类型"
            <<"状态"
           <<"供应商名称"
          <<"联系人"
         <<"电话"
        <<"订单号"
       <<"应收发票"
      <<"应付货款";

    ticketIndex = 8;
    moneyIndex = 9;

    header = headerClient;
    setHeader();
}

void MessageStandardItemModel::setType(int type){
    this->type = type;
    if(type == UserRelation::Client){
        header = headerClient;
    }else{
        header = headerGiver;
    }

    setHeader();
}

void MessageStandardItemModel::setHeader(){
    setHorizontalHeaderLabels(header);
}

void MessageStandardItemModel::reset(){
    clear();
    messages.clear();
    setHeader();
}

void MessageStandardItemModel::insertMessage(Message &message){
    QList<QStandardItem*> rowData;

    UserRelation *relation = message.getUserRelation();

    if(relation->isPlatform == 0){
        //非平台用户不显示
        return;
    }

    rowData.append(new QStandardItem(message.date));
    rowData.append(new QStandardItem(message.messageId));
    rowData.append(new QStandardItem(message.getMessageTypeStr()));
    rowData.append(new QStandardItem(message.isSend?"已发送":"已接收"));
    rowData.append(new QStandardItem(relation==nullptr?"":relation->nickName));
    rowData.append(new QStandardItem(relation==nullptr?"":relation->contact));
    rowData.append(new QStandardItem(relation==nullptr?"":relation->tel));
    rowData.append(new QStandardItem(message.dingdanId));
    int tempType;
    if(message.isSend){
        tempType = type;
    }else{
        tempType = type==1?2:1;
    }
    if(tempType == UserRelation::Client){
        rowData.append(new QStandardItem(QString::number(message.ticketOut, 'f', 2)));
        rowData.append(new QStandardItem(QString::number(message.moneyIn, 'f', 2)));
    }else{
        rowData.append(new QStandardItem(QString::number(message.ticketIn, 'f', 2)));
        rowData.append(new QStandardItem(QString::number(message.moneyOut, 'f', 2)));
    }

    appendRow(rowData);
    messages.append(&message);
}

void MessageStandardItemModel::insertMessage(QList<Message*> messages){
    for(Message *message:messages){
        insertMessage(*message);
    }

    refreshTotalValue();
}

void MessageStandardItemModel::refreshTotalValue(){
    double totalTicket = 0.0;
    double totalMoney = 0.0;

    for(int i=0; i<rowCount(); i++){
        QStandardItem *curTicketItem = item(i, ticketIndex);
        QStandardItem *curMoneyItem = item(i, moneyIndex);
        if(curTicketItem == nullptr || curMoneyItem == nullptr){
            break;
        }
        totalTicket += curTicketItem->text().toDouble();
        totalMoney += curMoneyItem->text().toDouble();
    }

    emit updateTotalValue(totalTicket, totalMoney);
}
